<?php
defined('IN_SITE') or exit('Access Denied');

require_once 'group.class.php';
$group = new group();
require_once MOD_ROOT.'attachment/attachment.class.php';
$attachment = new attachment($module, $catid);
$C = subcat('group');

if($groupid || $prefix){
	$where = $groupid ? " groupid = $groupid " : " prefix = '$prefix' ";
	$result = $db->get_one("select `groupid`,`status` from ".DB_PRE."group where $where ");
	//extract($result);
	$groupid = $result['groupid'];
	$status = $result['status'];
	unset($result,$where);
}

			
switch($action)
{
	
	case 'tag':
		$keyword = load('keyword.class.php','keyword');
		$data = $keyword->get($tag);
		if(!$data) showmessage("TAG: $tag 不存在！");
		$keyword->hits($tag);
		extract($data);
		
		// 所有tag标签连成的字符串，用于sql查询的时候where in()
		$data2 = $db->select("select `tag` from table_group_tag t");
		foreach($data2 as $k=>$v){
			$tags .= !$tags ? $v['tag'] : ','.$v['tag'];
		}
		
		$head['title'] = $tag.'-'.$SETTING['sitename'];
		
		include template('tag.html');
	break;
	
	/*供一个地图AJAX获取信息用*/
	case 'ajax':
		$data = $db->get_one("select `title`,`description` from ".DB_PRE."group where `prefix` = '$prefix'");
		$return = $data['description'] ? $data['description'] : $data['title'];
		echo($return."<br/><a href=\"".url("?module=group&prefix=$prefix")."\">进入小组</a>");
	break;
	
	/*case 'add':
		if($dosubmit)
		{
			$info['catid'] = $catid;
			$info['status'] = 99;
			if(isset($info['inputtime'])) $info['updatetime'] = $info['inputtime'];
			$groupid = $group->add($info,$cat_selected);
			if($groupid) showmessage('发布成功！', url("?module=$module&file=$file&action=add"));
		}
		else{
			$modelid = 55;
			require_once 'form.class.php';
			require 'model/group_form.class.php';
			$group_form = new group_form($modelid);
			$data['catid'] = $catid;
			$forminfos = $group_form->get($data);
			
			//dump($forminfos);
			include template('add.html','group');
		}
	break;*/
	
	// 加入小组
	case 'join':
		
		if(!$_userid) showmessage('请先登录！', url("?module=member&action=login"));
		
		if($groupid){
			$r = $db->get_one("select * from ".DB_PRE."group_member where userid = $_userid and groupid = $groupid");
			if($r) showmessage('已经加入了！', url("?module=$module&groupid=$groupid"));
			
			$data['groupid'] = $groupid;
			$data['userid'] = $_userid;
			$db->insert(DB_PRE.'group_member',$data);
			showmessage('加入成功！', url("?module=$module&groupid=$groupid"));
		}
	break;
	
	// 点击率，调用方法
	//<script language="JavaScript" src="?module=codex&action=count&codexid={$codexid}">...
	case 'count':
		if($groupid)
		{
			$groupid = intval($groupid);
			$r = $db->get_one("SELECT * FROM `".DB_PRE."group` WHERE `groupid`=$groupid");
			if(!$r) return false;
			$hits = $r['hits'] + 1;
			return $db->query("UPDATE `".DB_PRE."group` SET `hits`=$hits WHERE `groupid`=$groupid");
			
		}
		echo "\$('#hits').html($hits)";
	break;
	
	// 小组介绍
	case 'introduce':
	
		
		// 主要获取该文章的栏目所在信息，例如详细页模板等等
		require_once 'group.class.php';
		$group = new group();
		$r = $group->get($groupid);
		$C = cache_read('category_'.$r['catid'].'.php');
		
		// 获取该文章的所有内容信息
		require_once 'model/group_output.class.php';
		$out = new group_output();
		$data = $out->get($r);
		extract($data);
	
		// 设置文章的头部信息、关键字等
		$title = strip_tags($title);
		$head['title'] = $title.'_'.$C['catname'].'_'.$SETTING['sitename'];
		$head['keywords'] = str_replace(' ', ',', $r['keywords']);
		$head['description'] = $r['description'];
		
		include template('introduce.html', $module);
	break;
	
	
	// 小组成员
	case 'member':
		
		
		// 主要获取该文章的栏目所在信息，例如详细页模板等等
		require_once 'group.class.php';
		$group = new group();
		$r = $group->get($groupid);
		$C = cache_read('category_'.$r['catid'].'.php');
		
		// 获取该文章的所有内容信息
		require_once 'model/group_output.class.php';
		$out = new group_output();
		$data = $out->get($r);
		extract($data);
		
		// 设置文章的头部信息、关键字等
		$title = strip_tags($title);
		$head['title'] = $title.'_'.$C['catname'].'_'.$SETTING['sitename'];
		$head['keywords'] = str_replace(' ', ',', $r['keywords']);
		$head['description'] = $r['description'];
		include template('member.html', $module);
	break;
	
	
	// 更多讨论
	case 'discussion':
		
		
		
		// 主要获取该文章的栏目所在信息，例如详细页模板等等
		require_once 'group.class.php';
		$group = new group();
		$r = $group->get($groupid);
		$C = cache_read('category_'.$r['catid'].'.php');
		
		// 获取该文章的所有内容信息
		require_once 'model/group_output.class.php';
		$out = new group_output();
		$data = $out->get($r);
		extract($data);
		
		$pagesize = $M['pagesize'] ?  $M['pagesize'] : 20 ;
		$page = $page ? $page : 1 ;
		
		$condition = " status=99 ";
		if($keyword) $condition .= " and `title` like '%".$keyword."%'";
		
		$glist = $group->listinfo($condition,' updatetime DESC ',$page,$pagesize); 
		$pages = $group->pages;
		
		// 设置文章的头部信息、关键字等
		$title = strip_tags($title);
		$head['title'] = $title.'_'.$C['catname'].'_'.$SETTING['sitename'];
		$head['keywords'] = str_replace(' ', ',', $r['keywords']);
		$head['description'] = $r['description'];
		include template('discussion.html',$module);
	break;
	
	// 申请小组
	case 'create':
		
		if(!$_userid) showmessage("请登录后在操作！",url("?module=member&action=login"));
		if($dosubmit)
		{
			$info['catid'] = $catid;
			$info['userid'] = $_userid;
			$info['status'] = $_userid == 1 ?  99 : 3;
			if(isset($info['inputtime'])) $info['updatetime'] = $info['inputtime'];
			$groupid = $group->add($info,$cat_selected);
			if($groupid) showmessage('提交成功！现在为您返回小组首页', url("?module=$module"));
		}
		else{
			$modelid = 55;
			require_once 'form.class.php';
			require 'model/group_form.class.php';
			$group_form = new group_form($modelid);
			$data['catid'] = $catid;
			$forminfos = $group_form->get($data);
			
			//dump($forminfos);
			include template('create.html',$module);
		}
	break;
	
	// 指导方针
	case 'guide':
		
		include template('guide.html',$module);
	break;
	
	// 免责申明
	case 'disclaimer':
		
		include template('disclaimer.html',$module);
	break;
	
	// 邀请好友
	case 'invite':
	
		
		include template('invite.html',$module);
	break;
	
	// 回收站
	case 'recycle':
		require_once 'topic.class.php';
		$topic = new topic();
		$data = $topic->listinfo("groupid=$groupid AND status=0", 'listorder DESC,topicid DESC', $page, 20);
		include template('recycle.html',$module);
	break;
	
	// 统计
	case 'statistics':
	
		
		include template('statistics.html',$module);
	break;
	
	// 管理
	case 'manage':
	
		
		include template('manage.html',$module);
	break;
	
	// 群组设置
	case 'setting':
		
		if($dosubmit)
		{
			$info['catid'] = $catid;
			$info['status'] = ($status == 2 || $status == 3) ? $status : 99;
			$group->edit($groupid, $info);

			showmessage('修改成功！', url("?module=$module&groupid=$groupid"));
		}
		else
		{
			if(is_numeric($groupid) && $groupid>0)
			{
				$data = $group->get($groupid);
				$catid = $data['catid'];
				//$modelid = $CATEGORY[$catid]['modelid'];
			}
			$modelid = 55;

			require_once 'form.class.php';
			require 'model/group_form.class.php';
			$group_form = new group_form($modelid);
			$forminfos = $group_form->get($data);
			include template('setting.html',$module);
		}
		break;
	break;
	
	// 活动
	case 'event':
	
		include template('event.html',$module);
	break;
	
	// 友情小组
	case 'link':
	
		include template('link.html',$module);
	break;
	
	// 个性域名
	case 'domain':
	
		if($dosubmit){
			$groupid = intval($groupid);
			$prefix = strip_tags($domain);
			
			if(in_array($prefix, array('group','introduce','member','discussion','join','invite','recycle','statistics','manage','setting','event','link','domain','priv','dissolve','apply','explore','disclaimer','guide','topic','transfer','rss'))) 
			{
				showmessage('此个性域名受保护，不允许注册！',$forward);
			}
			if(!$group->is_prefix($prefix)) 
			{
				showmessage('个性域名不符合规范！',$forward);
			}
			else if($result = $db->get_one("SELECT `prefix` FROM $group->table WHERE prefix='$prefix'"))
			{
				showmessage('个性域名已经存在！',$forward);
			}
			else
			{
				$sql = "UPDATE `$group->table` SET `prefix` = '$prefix' WHERE groupid=$groupid";
				if($db->query($sql)){
					showmessage('修改成功！', $forward);
				}
				else{
					showmessage('修改失败！', $forward);
				}
			}		
		}
		else{
			$result = $db->get_one("SELECT `prefix` FROM $group->table WHERE groupid = $groupid");
			$domain = $result['prefix'];
			include template('domain.html',$module);
		}
	break;
	
	// 成员加入方式 （权限）
	case 'priv':
	
		include template('priv.html',$module);
	break;
	
	// 转让小组
	case 'transfer':
  		
		if($dosubmit){
			$userid = $member->get_userid($username);
			if(!$userid) exit($username.'会员不存在');
			if($userid==$_userid) exit('不能转给本人！');
			$result = $member->get($userid, 'disabled');
			if($result['disabled']) exit($username.'已经被禁用');
			
			$group->edit($groupid,array('userid'=>$userid));
			showmessage('转让成功！',url("?module=group&groupid=$groupid"));
		}
		else{
			include template('transfer.html',$module);
		}
		
	break;
	
	// 解散小组
	case 'dissolve':
		if($dosubmit)
		{
			$group->status($groupid, 0);
			showmessage('解散成功！现在为您跳回到首页！',SITE_URL);
		}
		else{
			include template('dissolve.html',$module);
		}
	break;
	
	// 发现小组
	case 'explore':
		include template('explore.html',$module);
	break;
	
	// 我的小组
	case 'mine':
		include template('mine.html',$module);
	break;
	
	//RSS
	case 'feed':
		require MOD_ROOT.'rss/rss.class.php';
		require_once 'group.class.php';
		$group = new group();
		$data = $group->get($groupid);
		
			
		$encoding   =  CHARSET;
		$about      =  SITE_URL;
		$title      =  $data['title'].'-'.$SETTING['sitename'];
		$description = $data['description'];
		$image_link =  $data['thumb'];
		$category   =  '';
		$cache      =  60;
		$rssfile    = new RSSBuilder($encoding, $about, $title, $description, $image_link, $category, $cache);
		$publisher  =  '';
		$creator    =  SITE_URL;
		$date       =  date('r');
		$rssfile->addDCdata($publisher, $creator, $date);
		$period     =  '';
		$frequency =  SITE_URL;
		$base =  date('Y');
		$rssfile->addSYdata($period, $frequency, $base);
		$sql = "SELECT `title`, `description`, `url`, `inputtime`, `thumb`, `keywords` FROM ".DB_PRE."group_topic WHERE `groupid` = $groupid AND `status` = '99' ORDER BY `inputtime` DESC LIMIT 0 , 20";
		$result = $db->query($sql);
		
		while ( $r = $db->fetch_array($result) )
		{
			
			$img = $r['thumb'] ? "<img src=".thumb($r['thumb'], 150, 150)." border='0' /><br />" : '';
			$tags = $r['keywords'] ? $r['keywords'] : '';
			$about          =  $link = url($r['url']); // url链接不要含连字符&
			$title          =  "<![CDATA[".$r['title']."]]>";
			$description    =  "<![CDATA[".$img.$r['description'].$tags."]]> ";
			$subject        =  '';
			$date           =  date('Y-m-d H:i:s' , $r['inputtime']);
			$author         =  $SETTING['sitename'].' '.SITE_URL;
			$comments       =  '';//注释
			$image          =  '';
			$rssfile->addItem($about, $title, $link, $description, $subject, $date,	$author, $comments, $image);
		}
		$version = '2.00';
		$rssfile->outputRSS($version);
	break;
	
	case 'latest':
		include template('latest.html',$module);
	break;
	
    default:
		
		if($groupid || $prefix){
			// 主要获取该文章的栏目所在信息，例如详细页模板等等
			require_once 'group.class.php';
			$group = new group();
			$r = $group->get($groupid);
			$C = cache_read('category_'.$r['catid'].'.php');
		
		
			
			// 获取该文章的所有内容信息
			require_once 'model/group_output.class.php';
			$out = new group_output();
			$data = $out->get($r);
			extract($data);
		
			// 设置文章的头部信息、关键字等
			$title = strip_tags($title);
			$head['title'] = $title.'_'.$C['catname'].'_'.$SETTING['sitename'];
			$head['keywords'] = str_replace(' ', ',', $r['keywords']);
			$head['description'] = $r['description'];
			 
			if(!$C['defaultchargepoint'] && !$r['readpoint'])
			{
				header('Last-Modified: '.gmdate('D, d M Y H:i:s', $r['updatetime']).' GMT');
				header('Expires: '.gmdate('D, d M Y H:i:s', $r['updatetime']+CACHE_PAGE_CONTENT_TTL).' GMT');
				header('Cache-Control: max-age='.CACHE_PAGE_CONTENT_TTL.', must-revalidate');
			}
			include template('group.html','group');
			cache_page(CACHE_PAGE_CONTENT_TTL);
		}
		else{
			$head['title'] = '小组_'.$SETTING['sitename'];
			$head['keywords'] = $meta_keywords;
			$head['description'] = $meta_description;
			
			$ttl = $child == 1 ? CACHE_PAGE_CATEGORY_TTL : CACHE_PAGE_LIST_TTL;
			header('Last-Modified: '.gmdate('D, d M Y H:i:s', TIME).' GMT');
			header('Expires: '.gmdate('D, d M Y H:i:s', TIME + $ttl).' GMT');
			header('Cache-Control: max-age='.$ttl.', must-revalidate');
			
			$pagesize = $M['pagesize'] ?  $M['pagesize'] : 20 ;
			$page = $page ? $page : 1 ;
			
            $condition = " status=99 ";
			if($keyword) $condition .= " and `title` like '%".$keyword."%'";
			
			$glist = $group->listinfo($condition,' updatetime DESC ',$page,$pagesize); 
			$pages = $group->pages;
			include template('index.html',$module);
		}
	break; 
}

// 
function group_check_status(){
	global $status;
	if($status != 99) showmessage("该小组不存在！正在为您跳转到首页",SITE_URL);
}
?>